package com.ruicar.afs.cloud.workflow.greatwall.processor.impl.objcode;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.workflow.greatwall.dto.GwtRequest;
import com.ruicar.afs.cloud.workflow.greatwall.entity.AfsGreatWallWorkflowNoticeException;
import com.ruicar.afs.cloud.workflow.greatwall.processor.AbsBaseProcessor;
import com.ruicar.afs.cloud.workflow.greatwall.service.AfsGreatWallWorkflowBizDataService;
import com.ruicar.afs.cloud.workflow.greatwall.service.AfsGreatWallWorkflowNoticeExceptionService;
import lombok.extern.slf4j.Slf4j;

/**
 * The type AbsGwfNoticeProcessor
 * <p>Description: </p>
 *
 * @author Fzero
 * @version 1.0
 * @date Created on 2020.07.10 21:22:46
 * @since 1.0
 */
@Slf4j
public abstract class AbsGwfNoticeProcessor extends AbsBaseProcessor {
    /**
     * Process key string
     *
     * @return the string
     */
    abstract String processKey();

    /**
     * Abs gwf notice processor
     *
     * @param afsGreatWallWorkflowBizDataService         afs great wall workflow biz data service
     * @param afsGreatWallWorkflowNoticeExceptionService afs great wall workflow notice exception service
     */
    public AbsGwfNoticeProcessor(AfsGreatWallWorkflowBizDataService afsGreatWallWorkflowBizDataService, AfsGreatWallWorkflowNoticeExceptionService afsGreatWallWorkflowNoticeExceptionService) {
        super(afsGreatWallWorkflowBizDataService);
        this.afsGreatWallWorkflowNoticeExceptionService = afsGreatWallWorkflowNoticeExceptionService;
    }

    /**
     * Afs great wall workflow notice exception service
     * <p>Description: </p>
     */
    protected final AfsGreatWallWorkflowNoticeExceptionService afsGreatWallWorkflowNoticeExceptionService;


    protected String genExceptionCheckCode(String value) {
        return SecureUtil.md5(value);
    }

    /**
     * Save exception *
     *
     * @param request request
     */
    protected void saveException(GwtRequest request, String flowInstanceId, String exceptionCheckCode) {
        if (afsGreatWallWorkflowNoticeExceptionService.count(Wrappers.<AfsGreatWallWorkflowNoticeException>lambdaQuery().eq(AfsGreatWallWorkflowNoticeException::getExceptionCheckCode, exceptionCheckCode)) > 0) {
            log.warn("异常已经记录，异常校验码{}", exceptionCheckCode);
            return;
        }
        AfsGreatWallWorkflowNoticeException afsGreatWallWorkflowNoticeException = new AfsGreatWallWorkflowNoticeException();
        afsGreatWallWorkflowNoticeException.setFlowServiceId(request.getServiceId());
        afsGreatWallWorkflowNoticeException.setBizDataId(request.getDataId());
        afsGreatWallWorkflowNoticeException.setNoticeInfo(JSON.toJSONString(request));
        afsGreatWallWorkflowNoticeException.setNoticeType(processKey());
        afsGreatWallWorkflowNoticeException.setFlowInstanceId(flowInstanceId);
        afsGreatWallWorkflowNoticeException.setExceptionCheckCode(exceptionCheckCode);
        afsGreatWallWorkflowNoticeExceptionService.save(afsGreatWallWorkflowNoticeException);
    }
}
